const {
  _navigateTo,
  formatTime
} = require('../../utils/util.js');
const {
  getUserInfo,
  getBalanceInfo
} = require('../../api/api.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    getUserInfo: {},
    balanceInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    if (app.userInfo) {
      this.setData({
        userInfo: app.userInfo
      })
    } else {
      wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
            wx.getUserInfo({
              success: res => {
                this.setData({
                  userInfo: res.userInfo
                })
                app.userInfo = res.userInfo;
                if (this.userInfoReadyCallback) {
                  this.userInfoReadyCallback(res)
                }
              }
            })
          } else {
            wx.reLaunch({
              url: '/pages/login/index',
            })
          }
        }
      })
    }
    /* 获取用户基本信息 */
    getUserInfo().then(res => {
      let vip_out_time = res.data.vip_out_time;
      if (vip_out_time) {
        const time = new Date(vip_out_time * 1000);
        vip_out_time = formatTime(time);
      } else {
        vip_out_time = '开通vip'
      }
      res.data.vip_out_time = vip_out_time;
      this.setData({
        getUserInfo: res.data
      })
    }).catch(err => {
      console.log(err)
    })
    /**
     *  获取余额信息
     */
    getBalanceInfo().then(res => {
      console.log(res)
      this.setData({
        balanceInfo: res.data
      })
    }).catch(err => {
      console.log(err)
    })
  },
  toOrder() {
    _navigateTo(`./order/index`);
  },
  toAllOrder() {
    _navigateTo(`./order/index?status=0`);
  },
  toWaitOrder() {
    _navigateTo("./order/index?status=1");
  },
  toReadyOrder() {
    _navigateTo("./order/index?status=2");
  },
  toUserInfo() {
    _navigateTo("./user-info/index");
  },
  toLooks() {
    _navigateTo("./look-record/index");
  },
  toIncome() {
    _navigateTo("./income/index");
  },
  toVip() {
    _navigateTo("./vip-center/index");
  },
  toexchangeCoupon() {
    _navigateTo("./conversion/index");
  },
  toGeneralize() {
    _navigateTo("./generalize/index");
  },
  toYetBuy() {
    _navigateTo("./yet-buy/index");
  },
  toMycoupon() {
    _navigateTo("./mycoupon/index");
  }
})